﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using OznerOA.BLL;
using OznerOA.Common;
using System.Threading;
using OznerOA.Utils;
using OznerOA.Model.Wechat;

namespace OznerOA.UI
{
    public partial class FrmBindWechat : FrmBase
    {



        private WechatBLL wechatBLL = new WechatBLL();
        private Thread threadWaitLogin = null;
        //线程是否停止
        bool stop = false;
        public FrmBindWechat()
        {
            InitializeComponent();
            this.Load += FrmBindWechat_Load;
        }
        #region 事件
        #region 窗体加载
        private void FrmBindWechat_Load(object sender, EventArgs e)
        {
            GetQrcode();
        }
        #endregion
        #region 窗体关闭
        private void FrmBindWechat_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!stop)
            {
                stop = true;
            }
        }
        #endregion
        #region 刷新二维码按钮点击
        private void btnRefreshQrcode_Click(object sender, EventArgs e)
        {
            GetQrcode();
        }
        #endregion
        #endregion
        #region 获取登录二维码
        private void GetQrcode()
        {
            ShowWaiting("正在请求登录二维码...");
            ControlDelegateSetProperty(lblCaption, "Text", "使用手机微信扫码登录");
            ControlDelegateSetProperty(lblTip, "Text", "登录微信需要配合手机使用");
            ControlDelegateSetProperty(btnRefreshQrcode, "Visible", false);
            //获取登录二维码，并显示在PictureEdit中
            BaseResult<Image> br = wechatBLL.GetLoginGrcode(true);
            if (br.Code == Code.OK)
            {
                pictureEdit1.Image = br.Object;
                //启动检测扫码
                checkQrcode();
            }

            CloseWaiting();
        }
        #endregion
        #region 检测是否扫码
        private void checkQrcode()
        {
            //启动检测是否扫码的线程
            threadWaitLogin = ThreadHelper.StartNewThread(() =>
            {
                //在获取登录状态的时候需要tip参数
                //当扫描还未点登录时，再获取tip要改为0
                string tip = "1";
                logBLL.Debug("已启动检测二维码登录线程!");
                while (!stop)
                {
                    logBLL.Debug("正在检测登录状态...");
                    BaseResult<Dictionary<string, string>> brInfo = wechatBLL.GetLoginInfo(tip);
                    tip = "0";
                    if (brInfo.Code == Code.OK)
                    {
                        string code = brInfo.Object["window.code"];
                        if (code == "201")
                        {
                            logBLL.Debug("已扫描登录二维码!");
                            //已扫描，未登录
                            string image = brInfo.Object["window.userAvatar"];
                            if (!string.IsNullOrEmpty(image))
                            {
                                Image img = ImageUtils.Base64ToImage(image.Replace("data:img/jpg;base64,", ""));
                                if (img != null)
                                {
                                    ControlDelegateSetProperty(pictureEdit1, "Image", img);
                                }
                            }
                            //设置提示文本
                            ControlDelegateSetProperty(lblCaption, "Text", "已扫描二维码!");
                            ControlDelegateSetProperty(lblTip, "Text", string.Empty);
                        }
                        if (code == "200")
                        {
                            logBLL.Debug("已成功登录微信!");
                            //关闭登录面板
                            ControlDelegateSetProperty(panelLogin, "Visible", false);
                            //显示主面板
                            ControlDelegateSetProperty(panelMain, "Visible", true);
                            //初始化
                            LoginedInit();
                            //已经登录
                            stop = true;
                        }
                        if (code == "400")
                        {
                            logBLL.Debug("验证码已失效!");
                            ControlDelegateSetProperty(btnRefreshQrcode, "Visible", true);
                            ControlDelegateSetProperty(lblCaption, "Text", "二维码已失效");
                            ControlDelegateSetProperty(lblTip, "Text", string.Empty);
                            //已经登录
                            stop = true;
                        }
                    }
                }
            }, true);
        }
        #endregion
        #region 登陆后初始化
        private void LoginedInit()
        {
            ShowWaiting("正在初始化...");
            if (panelMain.Visible)
            {
                //初始化
                BaseResult<Contract> br = wechatBLL.Init();
                //获取联系人列表
                //BaseResult<List<Contract>> br = wechatBLL.GetContracts();
            }
            CloseWaiting();
        }
        #endregion
    }
}